// feedback.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    feedTittle: '',
    feedContent: '',
    phoneOrMail: '',
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //反馈意见字段赋值
  blurFun: function (e){
    var param = e.target.dataset.param;
    var val = e.detail.value;
    var that = this;
    if(param=='t'){
      that.setData({feedTittle: val.trim()});
    }else if(param=='c'){
      that.setData({ feedContent: val.trim()});
    }else if(param=='p'){
      that.setData({ phoneOrMail: val.trim()});
    }
  },
  //提交意见反馈
  submitFeedBack: function () {
    var that = this;
    var tittle = that.data.feedTittle;
    var content = that.data.feedContent;
    var contact = that.data.phoneOrMail;
    if(tittle == ''){
      wx.showToast({
        title: '请输入意见反馈标题',
        image: '../../images/error.png',
        duration: 2000,
        mask:true
      })
      return false;
    }
    if (content == '') {
      wx.showToast({
        title: '请输入意见反馈内容',
        image: '../../images/error.png',
        duration: 2000,
        mask: true
      })
      return false;
    }
    var preg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var mreg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    if (contact == '') {
      wx.showToast({
        title: '请输入电话或邮箱',
        image: '../../images/error.png',
        duration: 2000,
        mask: true
      })
      return false;
    }else if(!preg.test(contact) && !mreg.test(contact)){
      wx.showToast({
        title: '电话或邮箱格式不正确',
        image: '../../images/error.png',
        duration: 2000,
        mask: true
      })
      return false;
    }
    var params = {
      tittle: tittle,
      content: content,
      contact: contact
    }
    that.setData({loading:true});
    wx.request({
      url: app.url + '/web/feedback/add.htm',
      data: app.ecbEncrypto(params),
      method: 'POST',
      dataType: 'json',
      success: function (res){
        that.setData({ loading: false });
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        if (rest.isSuccess == 0) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success:function(){
              setTimeout(function () {
                //要延时执行的代码
                wx.switchTab({url: '../my/my'})
              }, 2000) //延迟时间 
            }
          })
        }
      }
    })
  }
})